package com.mdxq.mall.user.controller;

import com.mdxq.mall.entity.Message;
import com.mdxq.mall.entity.Order;
import com.mdxq.mall.user.client.MessageService;
import com.mdxq.mall.user.client.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.awt.*;

@RestController
@RequestMapping("/api/v1/test")
public class TestController {
    @Autowired
    //自动装配一个RestTemplate实例,用来进行远程的服务调用
    private RestTemplate restTemplate;
    @Autowired
    private MessageService messageService;
    @Autowired
    private OrderService orderService;
    @GetMapping("/t2")
    public Order getOrder(Long id){
        return orderService.getInfo(id).getBody();
    }
    @GetMapping("/t1")// 采用feign的方法调用远程shop-product
    public Message getMessage1(Long id){
        ResponseEntity<Message> responseEntity = messageService.getInfo(id);
        Message message = responseEntity.getBody();
        message.setInfo("来自Feign客户端:"+message.getInfo());
        return message;
    }
    @GetMapping("/t0")
    public Message getMessage(Long id){
        //有注册中心
        String url = "http://shop-product/api/v1/message/getInfo/{id}";
        //发送调用请求 将结果自动反序列化成一个ResponseEntity实例
        ResponseEntity<Message> responseEntity = restTemplate.getForEntity(url, Message.class,id);
        return responseEntity.getBody();
    }
}
